$(document).ready(function(){
	//头像处理
	var pic =$("#FJID").text();
	if(pic!==""){
		$("#paListDIV").show();
	}else{
		$("#b_bialog").show();
	}
	
	$.ajax({
  		type: "post",
 		url: "hmk/getWukuType_getWukuType.action",
  		success:function(data){
  			$.each(data.typeList,function(i,n){
  				$("<option value="+n.tpyeId+">"+n.tpyeName+"</option>").appendTo('#wukutype_id');
			});
			//设置选中的select 			
			var type = $("#typeID").text();
			$("#wukutype_id option").each(function(){
				var v = $(this).attr("value");
				if(v==type){
					$(this).attr("selected","selected");
				}
			});
  		}
	});
	
	
	$.ajax({
  		type: "post",
 		url: "hmk/findExpertArea_findAll.action",
  		success:function(data){
  			$.each(data.expareaList,function(i,n){
  				$("<option value="+n.expareaId+">"+n.expareaName+"</option>").appendTo('#expertareaID');
			});
			//设置选中的select 			
			var type = $("#areaID").text();
			$("#expertareaID option").each(function(){
				var v = $(this).attr("value");
				if(v==type){
					$(this).attr("selected","selected");
				}
			});
  		}
	});
	
	//处理性别的选中情况
	var sexEn = $("#sexID").text();
	$("input[name='sex']").each(function(){
		var sex = $(this).val();
		if(sexEn==sex){
			$(this).attr("checked","checked");
		}
	});
	
	//加载专家状态
	$.ajax({
  		type: "post",
 		url: "admin/user_findAllUserState.action",
  		success:function(data){
  			$.each(data.userStates,function(i,n){
  				$("<option value="+n.usId+">"+n.usName+"</option>").appendTo('#status_id');
			});
			//设置选中的select 			
			var type = $("#Dis_usID").text();
			$("#status_id option").each(function(){
				var v = $(this).attr("value");
				if(v==type){
					$(this).attr("selected","selected");
				}
			});
  		}
	});
	
	//弹出对话框初始化	
	$("#dialog-form").dialog({
		autoOpen: false,
		width: 460,
		modal: true,
		resizable: false,
	    close: function() { //关闭弹出框后响应的函数
	    	$("#FJID").text("");
			$("#FJNameID").text("");
			//getPaList();
			var realName=$(window.frames["uploadFrame"].document).find("#RealNameID").text();
			var objectName=$(window.frames["uploadFrame"].document).find("#ObjectNameID").text();
			//alert(realName);
			//alert(objectName);
			$("#FJID").text(objectName);
			$("#FJNameID").text(realName);
			$("#b_bialog").hide();
			$("#paListDIV").show();
	 	}
	});
	
	//打开头像上传对话框
	$("#b_bialog").click(function(){
		$("#uploadFrame").attr("src","/ag/hmk/uploadPic.jsp");
		$('#dialog-form').dialog('open'); //open dialog
	});
	
	/////////////////////
	initvalidator(); //验证所需
	$("#acceptForm").validate({
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
            //form.submit();   //提交表单
        var experdID=$("#expertID").text();		
		var typeID=$('#wukutype_id option:selected').val();
		var areaID=$('#expertareaID option:selected').val();
		var expNameID=$('#expNameID').val();
		var expBirthdayID=$('#expBirthdayID').val();
		var expSexID=$("input[name='sex']:checked").val();
		var expDegreeID=$('#expDegreeID').val();
		var expEdutID=$('#expEdutID').val();
		var expPostID=$('#expPostID').val();
		var expRusumeID=$('#expRusumeID').val();
		var expSchoolID=$('#expSchoolID').val();
		var expTitleID=$('#expTitleID').val();
		var expUnitID=$('#expUnitID').val();
		var expProID=$('#expProID').val();
		var expPhoneID=$('#expPhoneID').val();
		
		var status_id=$('#status_id option:selected').val();
		var pwd=$('#pwdID').val();
		var fax=$('#expFaxID').val();
		var dianhua=$('#expDianHuaID').val();
		var email=$('#expEmailID').val();	
	
		var FJID =$("#FJID").text();
		var FJNameID =$("#FJNameID").text();
		
		var depts = $("#ud_udIds").text();
		if(depts===""){
				alert("所属部门一定要选择！");
				return;
		}
		//alert("部门："+depts);
		var roles = $("#r_rIds").text();
		if(roles===""){
			alert("所属角色一定要选择！");
			return;
		}
		//alert("角色:"+roles);
		if(depts=="00")
				depts=""
		if(roles=="00")
				roles=""
				
				
		////
		//alert(experdID+","+expNameID+","+pwd+","+depts+","+roles);
		//alert("状态："+status_id);
		$.ajax({
				type : "post",
				data : {"user.UId":experdID,"user.UName":expNameID,
       					"user.UPassword":pwd,"user.UPhone":expPhoneID,
       					"user.UTel":dianhua, "user.UFax":fax,"user.UMail":email   
       					,"user.tbUserState.usId":status_id ,"selDepts":depts,"selRoles":roles
						 },
				url : "admin/user_updateExpertUser.action", //请求镇澎的用户更新action
				success : function(data){
					 if(data=="true"){
					 	 $.ajax({
  						    type: "post",
 							url: "hmk/saveExpert_updateExpert.action",
 							data:{
 				  			expertID1:experdID,type1ID:typeID,area1ID:areaID,expName1:expNameID,expBirthday1:expBirthdayID,
 				 	    	expSex1:expSexID,expDegree1:expDegreeID,expEdut1:expEdutID,expImg1:"",
 				        	expPost1:expPostID,expRusume1:expRusumeID,expSchool1:expSchoolID,expTitle1:expTitleID,
 				       		expUnit1:expUnitID,expPro1:expProID,expPhone1:expPhoneID,expEmail:email,
 				        	fileID1:FJID ,expStatusID:status_id ,expPwd:pwd,expFax:fax,expTel:dianhua,
 				        	fileNameID1:FJNameID
 						 },
  						 success:showData
						 });
						 
					 }else{
					 	alert("修改失败！");
					 }
				}
			});
		  
        },
		rules: {
			'expNameID':'required',
			'status_id':'required',
			'wukutype_id':'required',
			'expertareaID':'required',
			'rePwdID':{
				equalTo: "#pwdID"
			},
			'sex':'required',
			'status_id':'required',
			//'expBirthdayID':'required',
			'expPhoneID':{	
				required: true,
				isMobile:true
			},
			'expFaxID':{
				isTel:true
			},
			'expDianHuaID':{
				isTel:true
			},
			'expEmailID':{
				required: false,
				email:true
			}
		} ,
		messages: {
			'expNameID':'*必选项',
			'status_id':'*必选项',
			'wukutype_id':'*必选项',
			'expertareaID':'*必选项',
			'rePwdID':{
				equalTo: "密码要一致"
			},
			'sex':'*必选项',
			'status_id':'*必选项',
			//'expBirthdayID':'*必选项',
			'expPhoneID':'如13800138000',
			'expFaxID':'如020123456',
			'expDianHuaID':'如020123456',
			'expEmailID':{
				required: '',
				email:'请输入正确的格式'
			}
		}
	});
	
	//返回
	$("#back_EXP").click(function(){
		var experdID=$("#expertID").text();	
		var timestamp = Date.parse(new Date());
		window.location.href="/ag/hmk/findOneExpert_findOneExpert.action?expertID="+experdID+"&findType1=1&ts="+timestamp;
	});
	
});
	
	
	function showData(data){
		if(data.ajaxRST=='1'){
			alert("修改成功！");
			var experdID=$("#expertID").text();	
			window.location.href="/ag/hmk/findOneExpert_findOneExpert.action?expertID="+experdID+"&findType1=1";
		}else{
			alert("修改失败!");
		}
	}
	
	

	function getPaList(){	
		var timestamp = Date.parse(new Date());
		$.get("hmk/session_findPaList.action?ts="+timestamp,null,getPaListRst);
	}

	function getPaListRst(data){
		if(data.paList!=null){
			$.each(data.paList,function(i,n){
				$("#FJID").text(n.paId);
				$("#FJNameID").text(n.paName);
			});
			$("#paListDIV").show();
			$("#b_bialog").hide();
		}
	}	

	function delFJ(v){
		alert("删除成功");
		$("#FJID").text("");
		$("#FJNameID").text("");
		$("#b_bialog").show();
		$("#paListDIV").hide();
		//var timestamp = Date.parse(new Date());
		//$.get("hmk/session_DeleteSession.action?ts="+timestamp,null,delSessionOK);
	}
	
	function delSessionOK(data){
		alert("删除成功");
		$("#FJID").text("");
		$("#FJNameID").text("");
		$("#b_bialog").show();
		$("#paListDIV").hide();
	}
	
		function initvalidator() {
	
	jQuery.validator.addMethod("chinese", function (value, element) {
		//alert("chinese validator");
		var chinese = /^[\u4e00-\u9fa5]+$/;
		return this.optional(element) || chinese.test(value);
	}, "\u8bf7\u8f93\u5165\u4e2d\u6587");
	jQuery.validator.addMethod("isMobile", function (value, element) {
		//alert("Mobile validator");
		var length = value.length;
		var mobile = /^(((1[0-9][0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u624b\u673a\u53f7\u7801"); 
	
	// 电话号码验证      
	jQuery.validator.addMethod("isTel", function (value, element) {
		//alert("Tel validator"); 
		var tel = /^\d{3,4}-?\d{7,9}$/;    //电话号码格式010-12345678  
		return this.optional(element) || (tel.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u7535\u8bdd\u53f7\u7801,\u683c\u5f0f010-12345678");
	
	// 联系电话(手机/电话皆可)验证  
	jQuery.validator.addMethod("isPhone", function (value, element) {
		//alert("Phone validator");
		var length = value.length;
		var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		var tel = /^\d{3,4}-?\d{7,9}$/;
		return this.optional(element) || (tel.test(value) || mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u8054\u7cfb\u7535\u8bdd");
}
	